package net.sdd.school.service.impl;
import java.util.Date;
import java.util.List;

import net.sdd.school.entity.Course;
import net.sdd.school.entity.CourseDescription;
import net.sdd.school.entity.vo.CoursePublishVo;
import net.sdd.school.entity.vo.CourseVo;
import net.sdd.school.mapper.CourseMapper;
import net.sdd.school.service.ICourseDescriptionService;
import net.sdd.school.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2022-08-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseDescriptionService courseDescriptionService;

    @Override
    public String addCourse(CourseVo courseVo) {
        //1. 保存课程信息
        Course course = new Course();
        //1.1 对象拷贝，相同属性名称的才会拷贝；参数1：有数据库的对象；参数2：目标
        BeanUtils.copyProperties(courseVo,course);
        this.save(course);

        //2. 保存课程描述 （要能够拿到保存数据的主键）
        CourseDescription cd = new CourseDescription();
        //2.1 设置id
        cd.setId(course.getId());
        cd.setDescription(courseVo.getDescription());
        courseDescriptionService.save(cd);

        //3. 返回课程id
        return course.getId();
    }

    @Override
    public CourseVo findCourseById(String id) {
        //1. 根据课程id查询课程
        Course course = getById(id);
        //2. 根据课程id查询描述
        CourseDescription cd = courseDescriptionService.getById(id);
        //3. 封装vo
        CourseVo vo = new CourseVo();
        BeanUtils.copyProperties(course,vo);
        vo.setDescription(cd.getDescription());
        return vo;
    }

    @Override
    public void updateCourse(CourseVo courseVo) {
        //1. 修改课程信息
        Course course = new Course();
        BeanUtils.copyProperties(courseVo,course);
        this.updateById(course);
        //2. 修改课程描述
        CourseDescription cd = new CourseDescription();
        BeanUtils.copyProperties(courseVo,cd);
        courseDescriptionService.updateById(cd);
    }

    @Override
    public CoursePublishVo findCoursePublish(String courseId) {
        return baseMapper.findCoursePublish(courseId);
    }
}
